<template>
  <div class="type-nav">
    <div class="container">
      <div @mouseenter="mouseIsInNav = true" @mouseleave="mouseIsInNav = false">
        <h2 class="all">全部商品分类</h2>
        <div
          class="sort"
          @mouseenter="mouseIsInCategory = true"
          @mouseleave="mouseIsInCategory = false"
          v-show="sortIsShow"
        >
          <div
            class="all-sort-list2"
            @mouseleave="mouseEnterIndex = -1"
            @click="toSearch"
          >
            <div
              class="item"
              v-for="(category1, index) in category1List"
              :key="category1.id"
              @mouseenter="getCategory2Throttle(category1.id, index)"
            >
              <h3 :class="{ active: mouseEnterIndex === index }">
                <a
                  :data-categoryName="category1.name"
                  :data-category1Id="category1.id"
                  >{{ category1.name }}</a
                >
              </h3>
              <div class="item-list clearfix">
                <div class="subitem">
                  <dl
                    class="fore"
                    v-for="category2 in category1.children"
                    :key="category2.id"
                  >
                    <dt>
                      <a
                        :data-categoryName="category2.name"
                        :data-category2Id="category2.id"
                        >{{ category2.name }}</a
                      >
                    </dt>
                    <dd>
                      <em
                        v-for="category3 in category2.children"
                        :key="category3.id"
                      >
                        <a
                          :data-categoryName="category3.name"
                          :data-category3Id="category3.id"
                          >{{ category3.name }}</a
                        >
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { throttle } from "lodash";
import {
  reqCategory1List,
  reqCategory2List,
  reqCategory3List,
} from "@/api/home";
export default {
  name: "TypeNav",
  data() {
    return {
      //三级分类列表数据
      category1List: [],
      //保存鼠标移入的三级分类列表的某个的下标
      mouseEnterIndex: -1,
      //保存当前鼠标是否在三级分类区域
      mouseIsInCategory: false,
      //保存当前鼠标是否在 新创建的div区域
      mouseIsInNav: false,
    };
  },
  mounted() {
    //1.初始化发送一级分类请求
    this.getCategory1();
    //2.使用lodash对getCategory2函数 进行节流处理
    this.getCategory2Throttle = throttle(this.getCategory2, 50, {
      leading: true, //控制节流开始前是否调用
      trailing: true, //指定调用在节流结束后
    });
  },

  methods: {
    //1.封装初始化一级分类请求函数
    async getCategory1() {
      const result = await reqCategory1List();
      this.category1List = result;
    },

    //2.封装请求二级分类的请求函数
    async getCategory2(id, index) {
      //鼠标移入之后，修改保存当前移入下标的数据 为当前的id
      this.mouseEnterIndex = index;

      //1.做一个判断,判断当前id的一级分类数据有没有children属性
      const nowCategory1 = this.category1List.find((item) => item.id === id);
      if (nowCategory1.children) return;
      //2.根据一级分类请求到对应的二级分类结果
      const result = await reqCategory2List(id);
      // console.log(result);
      // console.log(result);
      //因为不同的一级分类得到的二级分类结果不一样,所以我们可以把二级分类的结果 在对应的一级分类数据中新增一个属性保存
      this.category1List.forEach((category1) => {
        if (category1.id === id) {
          //下边这种方式没有响应式
          // category1.children = result;
          //我们想要添加数据,要使用 this.$set()方法响应式的新增属性
          this.$set(category1, "children", result);

          //因为后端接口没有提供好,所以我们需要在设置完二级分类后,需要遍历二级分类的数据获取对应的三级分类(好的接口,刚才访问一级分类的时候,就会把对应的二级分类和三级分类一起给我们)
          result.forEach(async (category2) => {
            const result = await reqCategory3List(category2.id);
            //响应式给的当前二级分类数据扩展一个children方法,保存三级分类数据
            this.$set(category2, "children", result);
          });
        }
      });
    },
    //3.封装一个请求二级分类的 节流函数 的占位
    getCategory2Throttle() {},

    //4.编程式路由导航做跳转search
    toSearch(e) {
      //事件委托需要使用event中的target来判断点击的元素
      if (e.target.nodeName !== "A") return;
      //获取当前元素上保存的id和name 用来传递给search
      const { categoryname, category1id, category2id, category3id } =
        e.target.dataset;
      // console.log(categoryname, category1id, category2id, category3id);
      //编程式路由导航的时候 进行路由跳转 并携带query参数

      this.$router.push({
        name: "Search",
        query: {
          categoryName: categoryname,
          category1Id: category1id,
          category2Id: category2id,
          category3Id: category3id,
        },
        //看一下有没有关键字参数,如果有,也带着一起
        params: this.$route.params,
      });
      //每次点击切换三级分类之后 关闭下拉
      this.mouseIsInNav = false;
    },
  },
  computed: {
    sortIsShow() {
      //只要是home组件,则sortIsShow的值永远都是true
      if (this.$route.name === "Home") return true;
      //不是home区域,则sortIsShow的值可能是true 也可能是false,由鼠标是否在 三级分类区域控制
      return this.mouseIsInNav;
    },
  },
};
</script>

<style scoped lang="less">
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;
            &.active {
              background-color: pink;
            }
            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &:hover {
            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}
</style>